﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace EmailHelper.tool
{
    public class SqlHelper
    {


        public static String resource;
        public static String IPResource;

   
        private static bool isWindowsMethos;

        public static bool IsWindowsMethos
        {
            get { return isWindowsMethos; }
            set { isWindowsMethos = value; }
        }
       



        

        private static SqlConnection connection;

        public static SqlConnection Connection
        {
            get 
            {
                if (connection == null)
                {
                    if (IsWindowsMethos)
                    {
                        connection = new SqlConnection(resource);
                    }
                    else
                    {
                        connection = new SqlConnection(IPResource);
                    }
                    
                }
                else if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
              
                return connection;
            }
          
        }


        public static DataTable Select(String tableName,String columnName)
        {
            try
            {
                String Sql = $"SELECT * FROM {tableName}";
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(Sql, Connection);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                return dataSet.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示");
                return new DataTable();
            }
            

        }
    }
}
